package org.fkjava.oa.identity.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.fkjava.common.dao.impl.BaseDaoImpl;
import org.fkjava.common.util.pager.PageModel;
import org.fkjava.oa.identity.dao.JobDao;
import org.fkjava.oa.identity.dao.ModuleDao;
import org.fkjava.oa.identity.dao.RoleDao;
import org.fkjava.oa.identity.domain.Module;
import org.fkjava.oa.util.OaContants;

/**
 * @author xlei
 * @Email dlei0006@163.com
 * @QQ 251425887
 * @Tel 18665616520
 * @Date 2017年1月14日上午9:44:47
 * @From http://www.fkjava.org 
 *
 */
public class ModuleDaoImpl extends BaseDaoImpl implements ModuleDao {

	@Override
	public List<Module> selectModuleByParent(String parentCode,PageModel pageModel) throws Exception {
		
		// 0001
		parentCode = parentCode==null?"":parentCode;   
		List<Object> values = new ArrayList<>();
		values.add(parentCode+"%");
		values.add(parentCode.length()+OaContants.CODE_LEN);
		// 子节点的编号的长度是父节点编号长度+步长
	    // 子节点前几位的编号必须与父节点编码一致
		String hql = "select m from Module m where m.code like ? and length(m.code) = ?";
		if(pageModel==null){
			return (List<Module>) find(hql.toString(), values.toArray());
		}
		return findByPage(hql, pageModel, values);
	}

	@Override
	public void deleteModules(String[] codes) throws Exception{
		StringBuilder hql = new StringBuilder();
		hql.append("delete Module m where ");
		Object[] params = new Object[codes.length];
		for(int i = 0 ; i < codes.length ; i++ ){
			params[i] = codes[i]+"%";
			hql.append(i==codes.length-1?" m.code like ? ":" m.code like ? or ");
		}
		bulkUpdate(hql.toString(), params);
	}
	
}







